import java.security.*;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.io.ByteArrayInputStream;
import java.util.Base64;

/**
 * 验证证书中的公钥是否和私钥匹配
 */
public class VerifyKeyPair {
    
    // a7参数中的证书
    private static final String CERT_BASE64 = 
        "MIIC6DCCAdCgAwIBAgIGAZlhQblOMA0GCSqGSIb3DQEBCwUAMDUxCzAJBgNVBAYT" +
        "AkNOMRAwDgYDVQQKEwdFeGFtcGxlMRQwEgYDVQQDEwtTZWxmLVNpZ25lZDAeFw0y" +
        "NTA5MTkwOTE1MjFaFw0yNjA5MTkwOTE1MjFaMDUxCzAJBgNVBAYTAkNOMRAwDgYD" +
        "VQQKEwdFeGFtcGxlMRQwEgYDVQQDEwtTZWxmLVNpZ25lZDCCASIwDQYJKoZIhvcN" +
        "AQEBBQADggEPADCCAQoCggEBALPwnK6bMFFHWeN3E/MIWmpZesBQGILjJxNtMN1Z" +
        "2lmRyLCJdTmz87EXmLuX4e/Bz0aCOmH88qjs+laHFmnPIQal3rJU32MCMk63bejZ" +
        "N6yArwv8MAyF0kvyA3EoBC6ZsYX55sxtxLyNYvG8MCqmacRbxV0IsSMXq6o4+1wO" +
        "YiQEXzdbqhp67fYPNOv81LXqxQZArGV+Wj2o86XtW1wixtHnaSUori4jJjbe1YKi" +
        "6LIbzhz9SK34+V4PkRhQYS7ensgAAbm4k3N5R3RSNkOHLrHu576QaBwKYI1DjNJL" +
        "9tAcySdqXColS9RLG0h/x2r78Pvybt3hm32f1kPbu3hnaMcCAwEAATANBgkqhkiG" +
        "9w0BAQsFAAOCAQEApMy1ilW+EsqQZ+2VMyvmQgjfqoaat5kvNqdluLwtzISuY/dv" +
        "1V2ccR3eShnta7hKO1WCT1WF3L31MLtDwWw/VEiWFDu3+H/9P9nWr7w6ocGVWCUw" +
        "UoZwiJk5DuBKZYlmj8LgjAKk6bh3hi7Fs3Kbct1rLXUtL0wiqRF7y49tnjWKiEOz" +
        "gIeHQnS6m2geS1aEpUQlsEm8vUanNuAmnWJppMpfJuWQ7XDPZfRDelyepkefWSdO" +
        "td3RKKWGhk70KM00Scmav4mrUYE1K8xIXUvYrc2ECpyvuJjZ6masXzoTRGN+bEvl" +
        "OML/U6TTbXIttbqqnNe9iLrszUStBXjGkpJohA==";
    
    // 我们的私钥
    private static final String PRIVATE_KEY_PEM = 
        "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCz8JyumzBRR1nj" +
        "dxPzCFpqWXrAUBiC4ycTbTDdWdpZkciwiXU5s/OxF5i7l+Hvwc9Ggjph/PKo7PpW" +
        "hxZpzyEGpd6yVN9jAjJOt23o2TesgK8L/DAMhdJL8gNxKAQumbGF+ebMbcS8jWLx" +
        "vDAqpmnEW8VdCLEjF6uqOPtcDmIkBF83W6oaeu32DzTr/NS16sUGQKxlflo9qPOl" +
        "7VtcIsbR52klKK4uIyY23tWCouiyG84c/Uit+PleD5EYUGEu3p7IAAG5uJNzeUd0" +
        "UjZDhy6x7ue+kGgcCmCNQ4zSS/bQHMknalwqJUvUSxtIf8dq+/D78m7d4Zt9n9ZD" +
        "27t4Z2jHAgMBAAECggEAAr15RVdrpvE1NzeLADpyVghCzEbr+KJI6AzTn6tMneyQ" +
        "Z8/QDy7kWSAI3WJ0uFf1Nhepl/BoKZZiQYsRFk9nK1i/SWvtcu6HoZc9fzw/ksrq" +
        "333ZpXcsOqfW0ZRQa/0/LNEfaKGLS2vDw/afrSaXmbvkB4SoXeZwYMk5Wq+FYxL/" +
        "alrNpg/lzdvBlsCsTDA0G2RZrUVaO5yRTLRisBU+i8yORgnGkEwWvKpZYSFogCHS" +
        "iYHiNXFy//C+sYWlK7DjmG8/+krKhqBRRfdeg7LFThHQpbEGTtueD57jw0PCo2yr" +
        "6dqTvW9Qys+aWVNJk7RHNaI0yBFKWPadaHi3/olzkQKBgQDmz/FDpztS0fjKDIOj" +
        "gDyE76Z6dRFenmKJM1eOFt+MniY6/vi4ylU51/Etm4k6dNJ1MVBoFfyT9X0U/4k7" +
        "GNNUfIxMCJztUgY6EaCCt+09wk8pIvmOlRCmtxGwvmVC4nJVgUj3XJQ+wseblAkk" +
        "W0+IuT3GKX0CdKFMU4q9aucLYwKBgQDHk3rQ8Ae4ci4oj+MehRI29SNpcXG6OHbr" +
        "Z2EdsgqiFC1o/qv0269jtmCP7IqkGMdhTQrVetTHjZNZJH2bvUZ4o/0YT2+TTFPI" +
        "kTb2/a0txUTBTKrbnAdXh/iPFZAGvBROEDN0MNKo/vGAfi0K836DeTyBIaYongm6" +
        "6Rn6pwnUTQKBgBveVaougfoxAhIbSrWuISCH8xjsE6nSA+G/Aj5Uwq8u1TzgVlWx" +
        "kHLIgQVZt0sImfSufJ/kr7eJt42WgRJSoAmedC4mCBSbh8bxI+lEne+MC5TS9UDi" +
        "/Ly0c/1cL8vQna93ScEcO4YMbJ97U1NBdyvx+eR4U/C89lDJ8YGHa9gzAoGAQ6sY" +
        "sHFCXOKyDeTDoFyEUYgKqrzhT7/HaofR4Oy2OEBZKUl4anx2WnvC/+m3FG6mY7Jo" +
        "ovuT29mABXCe+khR9aO8tBpy/WGa4t2B4nse1e8WIehp4i5kOuSKfZFVFUN+Kv3J" +
        "RHMtakmO/v9JLHZlBhT8U9hh61GygOJ6gYdTiN0CgYAFz9iPy/xqLOUwiTNp3Imz" +
        "hdZo9ol3Qlr9CfMWMFHqRA5AyYvly9ZzxTpUCpSA+qz6OXqAjJ0bhGQxW2KHpmcT" +
        "yNGOPPca2vCaW8Mb1NTQueXpUBqX7alIkrRjUUyULteLb8FtyYl7mR3TVIu3FoO5" +
        "anNwDZ+2y7R9WUEiOzY3NA==";
    
    public static void main(String[] args) {
        try {
            System.out.println("=" + "=".repeat(99));
            System.out.println("验证密钥对是否匹配");
            System.out.println("=" + "=".repeat(99));
            
            // 解析证书
            byte[] certBytes = Base64.getDecoder().decode(CERT_BASE64);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) cf.generateCertificate(
                new ByteArrayInputStream(certBytes)
            );
            
            PublicKey publicKeyFromCert = cert.getPublicKey();
            System.out.println("\n从a7证书中提取的公钥:");
            System.out.println("  算法: " + publicKeyFromCert.getAlgorithm());
            System.out.println("  格式: " + publicKeyFromCert.getFormat());
            System.out.println("  长度: " + publicKeyFromCert.getEncoded().length + " 字节");
            
            // 解析私钥
            byte[] privateKeyBytes = Base64.getDecoder().decode(PRIVATE_KEY_PEM);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            
            System.out.println("\n私钥信息:");
            System.out.println("  算法: " + privateKey.getAlgorithm());
            System.out.println("  格式: " + privateKey.getFormat());
            System.out.println("  长度: " + privateKey.getEncoded().length + " 字节");
            
            // 从私钥生成公钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            // 我们无法直接从私钥生成公钥，但可以通过签名验证来测试配对
            
            // 方法：使用私钥签名，用证书中的公钥验证
            Signature signature = Signature.getInstance("SHA256withRSA");
            byte[] testData = "test message".getBytes();
            
            signature.initSign(privateKey);
            signature.update(testData);
            byte[] signatureBytes = signature.sign();
            
            System.out.println("\n生成测试签名:");
            System.out.println("  签名长度: " + signatureBytes.length + " 字节");
            
            signature.initVerify(publicKeyFromCert);
            signature.update(testData);
            boolean verified = signature.verify(signatureBytes);
            
            System.out.println("\n密钥对验证结果:");
            if (verified) {
                System.out.println("  ✅ 成功！私钥和a7证书中的公钥是一对！");
            } else {
                System.out.println("  ❌ 失败！私钥和a7证书中的公钥不匹配！");
            }
            
            System.out.println("\n" + "=".repeat(100));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


